/**
 * 
 */
package com.school.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.sql.Date;
import java.text.SimpleDateFormat;

import com.school.entity.NewRegistration;

/**
 * @author KK
 *
 */
public class AdminManagerDaoImpl implements AdminManagerDao {

	/* (non-Javadoc)
	 * @see com.school.dao.AdminManagerDao#viewNewlyRegistered()
	 */
	@Override
	public ArrayList<NewRegistration> viewNewlyRegistered() {
		// TODO Auto-generated method stub
		
		Connection c1 = null;
		ResultSet rs1 = null;
		
		ArrayList<NewRegistration> newstud = new ArrayList<NewRegistration>();
		
		try {
			c1 = ConnectionUtil.getConnection();
			String q1 = "SELECT REFERENCE_NO, REG_DATE, STUDENT_NAME, NEW_CLASS, DOB, EXAM_DATE, EXAM_STATUS, INTERVIEW_DATE, INTERVIEW_STATUS FROM new_registration WHERE EXAM_DATE='NA' or (interview_date='NA' and exam_status!='fail');";                                                        
			PreparedStatement s1 = c1.prepareStatement(q1);
			rs1 = s1.executeQuery();
			
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd");
			Date sqlDate = null;			///////////////
			java.util.Date utilDate = null;		///////
									////////////////
			
			while (rs1.next()) {
				NewRegistration nreg = new NewRegistration();
				
	/*			try {			///////////////
					
					sqlDate = rs1.getDate(3);
					
					utilDate = new java.util.Date(sqlDate.getDate());
				}
				
				catch (Exception e) {
					System.out.println("inside exception of date conversion.....");
					e.printStackTrace();
				}				///////////////
	*/			
				nreg.setRef_no(rs1.getString(1));
				nreg.setReg_date(rs1.getDate(2));
				nreg.setStudent_name(rs1.getString(3));
				nreg.setNew_class(rs1.getString(4));
				nreg.setDob(rs1.getDate(5));
				nreg.setExam_date(rs1.getString(6));
				nreg.setExam_status(rs1.getString(7));
				nreg.setInterview_date(rs1.getString(8));
				nreg.setInterview_status(rs1.getString(9));
				
				newstud.add(nreg);
				
			}			
				
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		finally{
			ConnectionUtil.CloseDbResources(c1);
		}
		
		return newstud;
	}

	@Override
	public Boolean setExamDtNewlyRegistered(String tstdate,
			String[] schedule_examdt) {
		// TODO Auto-generated method stub
		Boolean estatus1 = false;
		Connection cc = null;
		try {
			cc = ConnectionUtil.getConnection();
			String q1 = "UPDATE new_registration SET EXAM_DATE=? WHERE REFERENCE_NO=?";
			
			PreparedStatement s1 = cc.prepareStatement(q1);
			
			s1.setString(1, tstdate);
			for(int i=0;i<schedule_examdt.length;i++)
			{
				s1.setString(2, schedule_examdt[i]);
			
				if(s1.executeUpdate()>0)
				{	estatus1 = true; 			
				}
				else{
					estatus1 = false;
					break;
				}
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		finally{
			ConnectionUtil.CloseDbResources(cc);
		}
		
		return estatus1;
	}

	@Override
	public Boolean setInterviewDtNewlyRegistered(String interviewdate,
			String[] schedule_intrviewdt) {
		// TODO Auto-generated method stub
		Boolean istatus1 = false;
		Connection cc = null;
		try {
			cc = ConnectionUtil.getConnection();
			String q1 = "UPDATE new_registration SET INTERVIEW_DATE=? WHERE REFERENCE_NO=?";
			
			PreparedStatement s1 = cc.prepareStatement(q1);
			
			s1.setString(1, interviewdate);
			for(int i=0;i<schedule_intrviewdt.length;i++)
			{
				s1.setString(2, schedule_intrviewdt[i]);
			
				if(s1.executeUpdate()>0)
				{	istatus1 = true; 			
				}
				else{
					istatus1 = false;
					break;
				}
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		finally{
			ConnectionUtil.CloseDbResources(cc);
		}
		
		return istatus1;
	}

	@Override
	public Boolean setExamResult(String result00, String[] set_exam_result) {
		// TODO Auto-generated method stub
		Boolean eres1 = false;
		Connection cc = null;
		try {
			cc = ConnectionUtil.getConnection();
			String q1 = "UPDATE new_registration SET EXAM_STATUS=? WHERE REFERENCE_NO=?";
			
			PreparedStatement s1 = cc.prepareStatement(q1);
			
			s1.setString(1, result00);
			for(int i=0;i<set_exam_result.length;i++)
			{
				s1.setString(2, set_exam_result[i]);
			
				if(s1.executeUpdate()>0)
				{	eres1 = true; 			
				}
				else{
					eres1 = false;
					break;
				}
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		finally{
			ConnectionUtil.CloseDbResources(cc);
		}
		
		return eres1;
	}

	@Override
	public Boolean setInterviewResult(String result00,
			String[] set_interview_result) {
		// TODO Auto-generated method stub
		Boolean iresult1 = false;
		Connection cc = null;
		try {
			cc = ConnectionUtil.getConnection();
			String q1 = "UPDATE new_registration SET INTERVIEW_STATUS=? WHERE REFERENCE_NO=?";
			
			PreparedStatement s1 = cc.prepareStatement(q1);
			
			s1.setString(1, result00);
			for(int i=0;i<set_interview_result.length;i++)
			{
				s1.setString(2, set_interview_result[i]);
			
				if(s1.executeUpdate()>0)
				{	iresult1 = true; 			
				}
				else{
					iresult1 = false;
					break;
				}
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		finally{
			ConnectionUtil.CloseDbResources(cc);
		}
		
		return iresult1;
	}

	@Override
	public ArrayList<NewRegistration> setExamStatusForm() {
		// TODO Auto-generated method stub
		Connection c1 = null;
		ResultSet rs1 = null;
		ArrayList<NewRegistration> newstud = new ArrayList<NewRegistration>();
		
		try {
			c1 = ConnectionUtil.getConnection();
			String q1 = "SELECT REFERENCE_NO, REG_DATE, STUDENT_NAME, NEW_CLASS, DOB, EXAM_DATE, EXAM_STATUS, INTERVIEW_DATE, INTERVIEW_STATUS FROM new_registration WHERE EXAM_STATUS != 'fail'";                                                        
			PreparedStatement s1 = c1.prepareStatement(q1);
			rs1 = s1.executeQuery();
			while (rs1.next()) {
				NewRegistration nreg = new NewRegistration();
				
				nreg.setRef_no(rs1.getString(1));
				nreg.setReg_date(rs1.getDate(2));
				nreg.setStudent_name(rs1.getString(3));
				nreg.setNew_class(rs1.getString(4));
				nreg.setDob(rs1.getDate(5));
				nreg.setExam_date(rs1.getString(6));
				nreg.setExam_status(rs1.getString(7));
				nreg.setInterview_date(rs1.getString(8));
				nreg.setInterview_status(rs1.getString(9));
				
				newstud.add(nreg);
			}			
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally{
			ConnectionUtil.CloseDbResources(c1);
		}
		return newstud;
	}

}
